Ext.define('SRMT.controller.Login', {
	extend: 'Ext.app.Controller',
	
	views: [
	        'Login',
	        'Viewport'
	],
	
	refs: [{
		ref: 'loginForm',
		selector: 'srmtLogin'
	},{
		ref:'edtUserID',
		selector: 'srmtLogin > #edtUserID'
	},{
		ref: 'edtPassword',
		selector: 'srmtLogin > #edtPassword'
	},{
		ref: 'btnReset',
		selector: 'srmtLogin #btnReset'
	},{
		ref: 'btnSubmit',
		selector: 'srmtLogin #btnSubmit'
	},{
		ref: 'viewport',
		selector: 'viewport'
	}],
	
	init: function() {
		this.control({
			'srmtLogin': {
				render: this.onRender
			},
			
			'srmtLogin > #edtUserID': {
			},
			
			'srmtLogin > #edtPassword': {
			},
			
			'srmtLogin #btnReset': {
				click: this.resetForm
			},
			
			'srmtLogin #btnSubmit': {
				click: this.submitForm
			}
		});
	},
	
	onRender: function() {
		
	},
	
	resetForm: function() {
		this.getLoginForm().getForm().reset();
	},
	
	submitForm: function() {
		var form = this.getLoginForm().getForm();
		var winLoginForm = this.getLoginForm().up('window');
		if(form.isValid()) {
			form.submit({
				url: 'php/module/authenticate/login.php',
				method: 'POST',
				async: false,
				waitMsg: 'Authenticating ...',
				timeout: 5000,
				success: function(form, action) {
					Ext.MessageBox.show({
						title: 'Login Success',
						msg: 'Hello <b>'+action.result.user.username+'</b>.<br>You\re now login.',
						buttons: Ext.MessageBox.OK,
						icon: Ext.MessageBox.INFO
						});
					winLoginForm.destroy();
                },
				failure: function(form, action) {
					Ext.MessageBox.show({
						title: 'Login Failed',
						msg: '<b>UserID</b> and <b>Password</b> not match!',
						buttons: Ext.MessageBox.OK,
						icon: Ext.MessageBox.ERROR
						});
				}
			});
			
		}
		this.getController('SRMT.controller.Viewport').onRender();
	}
});